﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text.RegularExpressions;
using System.Security.Cryptography;
using System.Text;


public partial class Control_PrivyInfonew : System.Web.UI.UserControl
{
    private Srvtools.WebDataSet WMaster;
    private int _eid;
    private bool _readonly;

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            wnSelf.Visible = !ReadOnly;

            InitializeComponent(true);
            WMaster.SetWhere("Employees.EmployeeId=" + Eid);

            Master.DataSource = WMaster;


            //int i = WMaster.GetRecordsCount();
        }
    }


    public int Eid
    {
        get
        {
            return _eid;
        }
        set
        {
            _eid = value;
        }
    }

    public bool ReadOnly
    {
        get
        {
            return _readonly;
        }
        set
        {
            _readonly = value;
        }
    }



    private void InitializeComponent(bool action)
    {
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Control_PrivyInfonew));
        this.WMaster = new Srvtools.WebDataSet();
        ((System.ComponentModel.ISupportInitialize)(this.WMaster)).BeginInit();
        // 
        // WMaster
        // 
        this.WMaster.Active = action;
        this.WMaster.AlwaysClose = true;
        this.WMaster.DataCompressed = false;
        this.WMaster.DeleteIncomplete = true;
        this.WMaster.Guid = null;
        this.WMaster.LastKeyValues = null;
        this.WMaster.Locale = new System.Globalization.CultureInfo("zh-CN");
        this.WMaster.PacketRecords = 100;
        this.WMaster.Position = -1;
        this.WMaster.RemoteName = "SEmployees.EmployeesSelf";
        this.WMaster.ServerModify = false;
        ((System.ComponentModel.ISupportInitialize)(this.WMaster)).EndInit();
    }


    protected void wnSelf_Command(object sender, CommandEventArgs e)
    {
        if (e.CommandName == "cmdFirst" || e.CommandName == "cmdPrevious"
            || e.CommandName == "cmdNext" || e.CommandName == "cmdLast")
        {
            Master.ExecuteSelect(FVSelf);
            DataBind();
        }
        else if (e.CommandName == "cmdAdd")
        {
            Master.ExecuteAdd(FVSelf);
            DataBind();
        }
        else if (e.CommandName == "cmdDelete")
        {
            Master.ExecuteSelect(FVSelf);
            DataBind();
        }
        else if (e.CommandName == "cmdApply" && FVSelf.AllValidateSucess)
        {
            Master.ExecuteSelect(FVSelf);
            DataBind();
        }

    }

    public bool MyCheck(object value)
    {
        object v = value;
        return false;
    }

    protected void Master_Updating(object sender, Srvtools.WebDataSourceUpdatingEventArgs e)
    {
        try
        {

            //string Pwd = e.UpdatingValues["Password"].ToString();
            //string rePwd = ((TextBox)FVSelf.FindControl("RePassword")).Text;

            //if (Pwd != "")
            //{
            //    if (Pwd != rePwd)
            //    {
            //        throw new Exception("更新失败！重复密码错误！");
            //    }

            //    Regex r = new Regex(@"^[a-zA-Z][a-zA-Z0-9_]{4,15}$");

            //    if (!r.IsMatch(Pwd))
            //    {
            //        throw new Exception("更新失败！密码修改错误！\\r\\r字母开头，允许5-16字节，允许字母数字下划线！");
            //    }
            //}
            //else
            //{
            //    e.UpdatingValues["Password"] = Master.CurrentRow["Password"].ToString();
            //}
            // 
            //txtpwd
            string Pwd = ((TextBox)FVSelf.FindControl("txtpwd")).Text;
            if (Pwd == "123")
            {
                e.UpdatingValues["Password"] = Pwd;
            }
            else
            {
                e.UpdatingValues["Password"] = Master.CurrentRow["Password"].ToString();
            }

            int ID = Convert.ToInt32(Master.CurrentRow["EmployeeId"]);
            object NAME = e.UpdatingValues["UsaName"];

            object[] ret = Srvtools.CliUtils.CallMethod("SEmployees", "GetEmpUsaNameRepeat", new object[] { ID, NAME });
            if (Convert.ToInt32(ret[1]) > 0)
            {
                e.UpdatingValues["UsaName"] = Master.CurrentRow["UsaName"].ToString();
                throw new Exception("更新失败！登陆名修改失败，已重复！");
            }
        }
        catch (Exception ex)
        {
            e.UpdatingValues["Password"] = Master.CurrentRow["Password"].ToString();
            e.UpdatingValues["UsaName"] = Master.CurrentRow["UsaName"].ToString();
            this.Page.ClientScript.RegisterStartupScript(GetType(), "Msg", "alert('" + ex.Message + "')", true);
            //throw;
        }
    }


    protected void repwd_Click(object sender, EventArgs e)
    {

        ((TextBox)FVSelf.FindControl("txtpwd")).Text = "123";

        ((Button)sender).Text = "重置密码完毕 密码:123";

        Page.ClientScript.RegisterStartupScript(GetType(), "Msg", "alert('重置密码完毕！请点击保存按钮，保存密码')", true);
    }
}

